package cn.jilian.trans.bizz.web;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jilian.common.domain.R;
import cn.jilian.common.dto.PageInfo;
import cn.jilian.common.dto.PageResult;
import cn.jilian.entity.EleOrderItem;
import cn.jilian.entity.Order;
import cn.jilian.entity.Unit;
import cn.jilian.sys.service.UnitService;
import cn.jilian.trans.bizz.dto.OrderListQuery;
import cn.jilian.trans.bizz.dto.TransOrder;
import cn.jilian.trans.bizz.service.OrderService;
import cn.jilian.trans.bizz.util.SessionUtil;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private UnitService unitService;
	
	@ApiOperation(value="货源单管理界面")
	@GetMapping("/order/mgr.html")
	public ModelAndView orderMgrPage() {
		ModelAndView mv=new ModelAndView("gui/tab");
		mv.addObject("title", "订单管理");
		mv.addObject("jsfile", "order.order_mgr");
		return mv;
	}
	
	@ApiOperation(value="分页查询货源订单")
	@PostMapping("/order/list")
	@ResponseBody
	public PageResult<Order> orderList(@RequestBody OrderListQuery query,HttpSession session){
		Long fid=SessionUtil.getUnitId(session);
		return orderService.query(fid, query.getName(), query.getStartDate(),
				query.getEndDate(), query.getFlag(), query.getPageInfo(),query.getSortInfo());
	}
	
	@ApiOperation(value="填写货源单界面")
	@GetMapping("/order/fill.html")
	public ModelAndView fillOrderPage() {
		ModelAndView mv=new ModelAndView("order/list");
		return mv;
	}
	
	@ApiOperation(value="新增货源单界面")
	@GetMapping("/order/add.html")
	public ModelAndView addOrderPage(HttpSession session) {
		Long unid=SessionUtil.getUnitId(session);
		Unit unit=unitService.findBySid(unid);
		ModelAndView mv=new ModelAndView("order/add");
		mv.addObject("fid",unid);
		mv.addObject("title", unit.getText()+"货源单");
		mv.addObject("supplier", unit.getText());
		return mv;
	}
	
	@ApiOperation(value="新增货源单项-直升电梯界面")
	@GetMapping("/orderItem/addEle.html")
	public ModelAndView addEleOrderItemPage(@RequestParam(name="fid")Long fid,HttpSession session) {
		Long unid=SessionUtil.getUnitId(session);
		ModelAndView mv=new ModelAndView("orderitem/addEle");
		mv.addObject("fid", fid);
		return mv;
	}
	
	@ApiOperation(value="新增货源单项-扶梯/人行步道界面")
	@GetMapping("/orderItem/addEsc.html")
	public ModelAndView addEscOrderItemPage(@RequestParam(name="fid")Long fid,HttpSession session) {
		Long unid=SessionUtil.getUnitId(session);
		ModelAndView mv=new ModelAndView("orderitem/addEsc");
		mv.addObject("fid", fid);
		return mv;
	}
	
	@ApiOperation(value="新增货源单")
	@PostMapping("/order/add")
	public R submitOrder(@Valid @RequestParam Order order,HttpSession session) {
		Long unid=SessionUtil.getUnitId(session);
		order.setFid(unid);
		int r=orderService.save(order);
		return r>0?R.ok:R.fail;
	}
	
	@ApiOperation(value="新增货源单项")
	@PostMapping("/orderItem/elevator/add")
	public R addElevatorOrderItem(@RequestParam EleOrderItem orderItem,HttpSession session) {
		try {
			orderItem =orderService.add(orderItem);
		}catch(Exception e) {
			return R.fail("新增出错");
		}
		return R.ok;
	}
	
	@ApiOperation(value="查询待处理货源单列表")
	@PostMapping("/order/query")
	public PageResult<TransOrder> queryUnHandledOrder(PageInfo page){
		
		return  null;
	}
	
	@ApiOperation(value="选择指定报价单生成待签订物流合同")
	@PostMapping("/contract/{postId}")
	public R createContract(@PathVariable(name="postId")Long postId,HttpSession session) {
		return R.ok;
	}

}
